package test

import java.io.File

import org.apache.commons.io.FileUtils
import tools.Read
import utils.{ExecCommand, Utils}

import scala.collection.JavaConverters._


object getMtData {


  def main(args: Array[String]): Unit = {

    getOgdrawCmd

  }


  def getNums = {
    val file = Read.readPath("D:\\藻类细胞器\\mtgff/AB546637.2.gb.gff").readLine

    val fasta = file.indexOf("##FASTA")
    val l = file.take(fasta).filter(!_.startsWith("#"))

    val line = l.map(_.split("\t"))
    val rRNA = line.count(x=>x(2) == "rRNA")
    val tRNA = line.count(x=>x(2) == "tRNA")
    val orf = file.count(_.contains("protein_id"))
    val atp = file.count(_.contains("ATP synthase"))
    val nadh = file.count(_.contains("NADH dehydrogenase"))
    val ribos = file.count(_.contains("ribosomal protein"))
    val gene = line.count(x=>x(2)=="gene")
    val other = gene-rRNA-tRNA
    println(rRNA,tRNA,orf,atp,nadh,ribos,gene,other)
  }

  /**
    * GB转GFF，用bp_genbank2gff3，南昌大学服务器可以运行
    */

  def getMtImg = {
    new File("D:\\藻类细胞器\\mtimg").listFiles.foreach { x =>
      val n = x.getName.split("-").head.toInt + 1
      FileUtils.moveFile(x, new File("D:\\藻类细胞器\\mtimg/mt" + n + ".png"))
    }
  }

  def getMtGb = {
    val n = new File("D:\\藻类细胞器\\mtgb").listFiles().map { x =>
      val l = Utils.readLines(x)
      val name = l.find(_.startsWith("VERSION")).get.split(" ").last
     // println(name)
    //  FileUtils.copyFile(x, new File("D:/藻类细胞器\\mtgb3/" + name + ".gb"))
      (name,x.getName)
    }
    println(n.length)

    n.groupBy(_._1).filter(_._2.length>1).map{x=>
      println(x._1,x._2.mkString("\t"))
    }
  }

  def getMtCsv = {
    new File("D:\\藻类细胞器\\mtcsv").listFiles().foreach { x =>
      val l = Utils.readLines(x)
      val header = l.head
      val index = header.split(",").indexOf("Sequence Name")
      val i = if(index != -1){
        index
      }else{
        header.split(",").indexOf("Document Name")
      }
      println(x.getName)
      val name =  if(i != -1){
        l(1).split(",")(i)
      }else{
        x.getName.split("_").last.dropRight(4)
      }
      FileUtils.copyFile(x, new File("D:/藻类细胞器\\mtcsv2/" + name +  ".csv"))
    }
  }


  def getFasta = {
    new File("D:\\藻类细胞器\\mtfa").listFiles().foreach { x =>
      val l = Utils.readLines(x)
      val name = l.head.drop(1).split(" ").head
      val line = Array(">" + name) ++ l.tail
      FileUtils.writeLines(new File("D:\\藻类细胞器\\mtfa2/" + name + ".fasta"), line.toBuffer.asJava)
    }
  }


  /**
    * 在南昌大学服务器运行
    */
  def getOgdrawCmd = {
    new File("D:/藻类细胞器/getMtOgdraw.sh").delete()
    new File("D:/藻类细胞器\\mtgb3/").listFiles().foreach { x =>

      val name = x.getName.dropRight(3)

      val com = "drawgenemap --infile /mnt/sdb/platform/tmp/PODB/mtgb3/" + x.getName + "  --format jpg --outfile=/mnt/sdb/platform/tmp/PODB/mtogdraw/" +name + " --force_circular;\n"

      FileUtils.writeStringToFile(new File("D:/藻类细胞器/getMtOgdraw.sh"),com,true)
    }
  }


  def getMtPepAndCds = {
    val p = "D:\\藻类细胞器\\mtgb3"

    new File(p).listFiles().foreach{x=>

  //    val command1 = "perl D:/藻类细胞器/藻类细胞器数据库构建/genbank_parser_v4.0.pl --type cds " + x.getAbsolutePath + ""
    //  val command2 = "perl D:/藻类细胞器/藻类细胞器数据库构建/genbank_parser_v4.0.pl --type pep " + x.getAbsolutePath + ""
      val command = "perl D:/藻类细胞器/藻类细胞器数据库构建/genbank_parser_v4.0.pl --type all " + x.getAbsolutePath + ""

      val exec = new ExecCommand()
      exec.exect(Array(command),   "D:\\藻类细胞器\\mtgb3" )

    }
  }

  def getJbDir = {
    val path ="D:\\藻类细胞器\\mtjb/"
    new File(path).listFiles().foreach{x=>
      val name = x.getName.split("gb").head.init

      if(!new File(path+name).exists()){
        new File(path+name).mkdir()
      }

      FileUtils.moveFile(x,new File(path+name+"/" + x.getName))

    }
  }

  def reGetJbDir = {
    val path ="D:\\藻类细胞器\\mtjb/"
    new File(path).listFiles().foreach { x =>

      try{

      val name = x.getName.split('.').head

      FileUtils.moveDirectory(x,new File(path + name))
      }catch {
        case e =>
          println(x.getName)
      }

    }
  }


  def getJbCmd = {

    new File("D:\\藻类细胞器/getMtJB.sh").delete()
    val path = "/mnt/sdb/xwq/Jbrowser/apache-tomcat-9.0.0.M11/webapps/jb/my_data/PODB/MT/"

    new File("D:\\藻类细胞器\\mtjb").listFiles().foreach { x =>


      val n = x.getName
      val sh = s"cd /mnt/sdb/xwq/Jbrowser/apache-tomcat-9.0.0.M11/webapps/jb/my_data/PODB/MT/$n;\n"+
        s"../../../../bin/prepare-refseqs.pl -fasta ./*.gb.seq;\n"+
        s"../../../../bin/prepare-refseqs.pl --conf data/trackList.json;\n"+
        s"../../../../bin/flatfile-to-json.pl --gff ./*.gb.gff --trackType FeatureTrack --trackLabel Annotation;\n"+
        s"../../../../bin/biodb-to-json.pl --conf data/trackList.json ;\n"

      FileUtils.writeStringToFile(new File("D:\\藻类细胞器/getMtJB.sh"),sh,true)
    }
  }


}
